package com.douqu.game.core.entity.ext.data.challenge;

import com.alibaba.fastjson.JSONObject;
import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.protobuf.SGCommonProto;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-03-13 19:38
 */
public class CampBattleBean extends EntityObject{
    /** 当前复活次数 */
    public final static int REVIVE_CUR_COUNT = 1;
    /** 最大复活次数 */
    public final static int REVIVE_MAX_COUNT = 2;
    /** 主将剩余HP比例 */
    public final static int MASTER_HP_RATE = 3;
    /** 军团剩余HP比例 */
    public final static int ARMY_HP_RATE = 4;
    /** 获得功勋 */
    public final static int GET_EXPLOIT = 5;
    /** 失败类型 */
    public final static int LOSE_TYPE = 6;

    /** 失败后->复活 */
    public final static int LOSE_TYPE_REVIVE = 1;
    /** 失败就失败了 */
    public final static int LOSE_TYPE_DEFAULT = 2;

    /**
     * 是否胜利
     */
    public boolean win;

    /**
     * 对方玩家流水号
     */
    public String targetIndex = "";

    public String targetName = "";

    public String targetAvatar = "";

    public int targetServerId;

    public int targetFC;

    /**
     * 参数
     */
    private Map<Integer, Integer> params;

    public CampBattleBean()
    {
        this.params = new HashMap<>();
    }


    public void loadFrom(ByteBuffer buffer)
    {
        this.win = buffer.readBoolean();
        this.targetIndex = buffer.readUTF();
        this.targetName = buffer.readUTF();
        this.targetAvatar = buffer.readUTF();
        this.targetServerId = buffer.readInt();
        this.targetFC = buffer.readInt();

        int size = buffer.readByte();
        for(int i = 0; i < size; i++)
        {
            params.put(buffer.readByte(), buffer.readInt());
        }
    }

    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeBoolean(win);
        buffer.writeUTF(targetIndex);
        buffer.writeUTF(targetName);
        buffer.writeUTF(targetAvatar);
        buffer.writeInt(targetServerId);
        buffer.writeInt(targetFC);

        buffer.writeByte(params.size());
        for(Map.Entry<Integer, Integer> entry : params.entrySet())
        {
            buffer.writeByte(entry.getKey());
            buffer.writeInt(entry.getValue());
        }
    }


    public SGCommonProto.CampBattleRecord.Builder parse()
    {
        SGCommonProto.CampBattleRecord.Builder builder = SGCommonProto.CampBattleRecord.newBuilder();
        if(win)
        {
            builder.setResultType(SGCommonProto.E_CAMP_BATTLE_RESULT_TYPE.RESULT_TYPE_WIN);
            builder.setExploit(getParam(GET_EXPLOIT));
            builder.setMasterHP(getParam(MASTER_HP_RATE));
            builder.setArmyHP(getParam(ARMY_HP_RATE));
        }
        else
        {
            int loseType = getParam(LOSE_TYPE);
            if(LOSE_TYPE_REVIVE == loseType)
                builder.setResultType(SGCommonProto.E_CAMP_BATTLE_RESULT_TYPE.RESULT_TYPE_REVIVE);
            else
                builder.setResultType(SGCommonProto.E_CAMP_BATTLE_RESULT_TYPE.RESULT_TYPE_LOSE);

            int reviveCount = getParam(REVIVE_CUR_COUNT);
            builder.setRevive((reviveCount<0?0:reviveCount) + "/" + getParam(REVIVE_MAX_COUNT));
        }

        builder.setTargetIndex(targetIndex);
        builder.setTargetAvatar(targetAvatar);
        builder.setTargetName(targetName);
        builder.setTargetFC(targetFC+"");

        return builder;
    }

    public void parseParam(JSONObject jsonObject)
    {
        for(Map.Entry<Integer, Integer> entry : params.entrySet())
        {
            if(entry.getKey() == GET_EXPLOIT)
            {
                jsonObject.put("getExploit", entry.getValue());
            }
            else if(entry.getKey() == LOSE_TYPE)
            {
                jsonObject.put("loseType", entry.getValue()==LOSE_TYPE_REVIVE?"revive":"lose");
            }
            else if(entry.getKey() == ARMY_HP_RATE)
            {
                jsonObject.put("armyHP", entry.getValue());
            }
            else if(entry.getKey() == MASTER_HP_RATE)
            {
                jsonObject.put("masterHP", entry.getValue());
            }
            else if(entry.getKey() == REVIVE_MAX_COUNT)
            {
                jsonObject.put("reviveMaxCount", entry.getValue());
            }
            else if(entry.getKey() == REVIVE_CUR_COUNT)
            {
                jsonObject.put("reviveCurCount", entry.getValue());
            }
        }
    }

    public void addParam(int key, int value)
    {
        params.put(key, value);
    }

    public int getParam(int key)
    {
        Integer object = params.get(key);
        return object == null ? 0 : object.intValue();
    }

    public Map<Integer, Integer> getParams() {
        return params;
    }

    public void setParams(Map<Integer, Integer> params) {
        this.params = params;
    }

    @Override
    public String toString() {
        return "{" +
                "win=" + win +
                ", targetIndex='" + targetIndex + '\'' +
                ", targetName='" + targetName + '\'' +
                ", targetAvatar='" + targetAvatar + '\'' +
                ", targetServerId=" + targetServerId +
                ", targetFC=" + targetFC +
                ", params=" + params +
                "} " + super.toString();
    }
}
